<?php


namespace Module\Example;

use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Nlp\V20190408\NlpClient;
use TencentCloud\Nlp\V20190408\Models\ChatBotRequest;
use ZM\Annotation\CQ\CQMessage;
use ZM\API\CQ;

class Chat
{

    /**
     * @CQMessage()
     */
    public function index()
    {
        if (strstr(ctx()->getMessage(), "[CQ:at,qq=203558954]")) {
            try {
                $cred = new Credential("AKIDkqhjU31CPTPVfjFIm3A5oNnkSvZyEH1W", "OVObZGgpjMjuOfhTzKOFpJYWCbleIhxx");
                $httpProfile = new HttpProfile();
                $httpProfile->setEndpoint("nlp.tencentcloudapi.com");

                $clientProfile = new ClientProfile();
                $clientProfile->setHttpProfile($httpProfile);
                $client = new NlpClient($cred, "ap-guangzhou", $clientProfile);

                $req = new ChatBotRequest();

                $message = str_replace("[CQ:at,qq=203558954] ", "", ctx()->getMessage());
                $params = "{\"Query\":\"$message\"}";
                $req->fromJsonString($params);


                $resp = $client->ChatBot($req);

                $reply = str_replace("腾讯小龙女", "600线的小可爱", $resp->Reply);

                ctx()->reply($reply);

                //ctx()->reply($resp->Reply);
            } catch
            (TencentCloudSDKException $e) {
                echo $e;
            }
        }
    }
}